package com.base.interview.devtest.exception;

import com.base.interview.devtest.constants.ResponseCodeEnum;
import com.base.interview.devtest.model.ResponseModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DevTestException.class)
    public ResponseEntity<ResponseModel> handleMoikiitosException(DevTestException e) {
        ResponseModel responseModel = ResponseModel.builder().code(ResponseCodeEnum.FAIL.code).msg(e.getMessage()).build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseModel);
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<ResponseModel> handleAuthenticationException(AuthenticationException e) {
        ResponseModel responseModel = ResponseModel.builder().code(ResponseCodeEnum.AUTH_FAIL.code).msg(e.getMessage()).build();
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(responseModel);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseModel> handleException(Exception e) {
        ResponseModel responseModel = ResponseModel.builder().code(ResponseCodeEnum.SERVER_ERROR.code).msg(e.getMessage()).build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseModel);
    }

}
